<?php 

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//include the facebook.php from libraries directory
require_once APPPATH.'libraries/facebook/facebook.php';

class Fblogin extends CI_Controller {

   public function __construct(){
        parent::__construct();
        $this->load->library('session');  //Load the Session 
        $this->config->load('facebook'); //Load the facebook.php file which is located in config directory
    }
    public function index()
    {
      $this->load->view('fblogin/main'); //load the main.php file for view
    }
    
    function logout(){
        $base_url=$this->config->item('base_url'); //Read the baseurl from the config.php file
        $this->session->sess_destroy();  //session destroy
        header('Location: '.$base_url);  //redirect to the home page
        
    }
    function login(){
        $this->load->library('form_validation');
        $this->load->model('news_model');
        $base_url=$this->config->item('base_url'); //Read the baseurl from the config.php file
        //get the Facebook appId and app secret from facebook.php which located in config directory for the creating the object for Facebook class
        $facebook = new Facebook(array(
        'appId'        =>  $this->config->item('appID'), 
        'secret'    => $this->config->item('appSecret'),
        ));
        
        $user = $facebook->getUser(); // Get the facebook user id 
        
        if($user){
            
            try{
                $user_profile = $facebook->api('/me');  //Get the facebook user profile data
                
                $params = array('next' => $base_url.'/index.php/fblogin/logout');
                
                $ses_user=array('User'=>$user_profile,
                   'logout' =>$facebook->getLogoutUrl($params)   //generating the logout url for facebook 
                );
                #insert user in mysql
                #check email exits
                    if($this->check_email($user_profile['email'])){
                        
                    }else{
                        $dataadd            =   array(
                            "name"          =>  $user_profile['name'],
                            "opentid"          =>  $user_profile['id'],
                            "username"      =>  $user_profile['username'],
                            "email"         =>  $user_profile['email'],
                            "link"          =>  $user_profile['link'],
                            "group"         =>  3,
                            "date"          =>  strtotime(date("Y-m-d H:i:s")),
                            "birthday"      =>  $user_profile['birthday'],
                            "gender"        =>  $user_profile['gender'],
                            "published"     => 1
                        );
                        $this->news_model->adduser($dataadd);
                        
                    }
                #end check email
                $this->session->set_userdata($ses_user);
                #header('Location: '.$base_url);
                redirect('http://localhost/fuckonline/index.php/fblogin');
            }catch(FacebookApiException $e){
                error_log($e);
                $user = NULL;
            }    
            #redirect(base_url('fbci/adduser'));    
        }    
        $this->load->view('main');
        
    }
    public function adduser(){
        #$this->load->model('user_model');
        #get session
        #$ses_user=$this->session->userdata('User');
            
    }
    public function check_email($str){
        $this->db->where('email',$str);
        $query  = $this->db->get('user');
        if($query->row()){
            return TRUE;
        }else{
            return FALSE;
        }
        
    }
    
}